{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "#url_office= 'https://raw.githubusercontent.com/irenekarijadi/RF-LSTM-CEEMDAN/main/Dataset/data%20of%20Office_Abigail.csv'\n",
    "url_office= './Dataset/data of Office_Abigail.csv'\n",
    "\n",
    "office= pd.read_csv(url_office)\n",
    "data_office= office[(office['timestamp'] > '2015-03-01') & (office['timestamp'] < '2015-06-01')]\n",
    "dfs_office=data_office['energy']\n",
    "datas_office=pd.DataFrame(dfs_office)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import warnings\n",
    "\n",
    "if not sys.warnoptions:\n",
    "    warnings.simplefilter('ignore')\n",
    "\n",
    "from PyEMD import CEEMDAN\n",
    "import numpy\n",
    "import math\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense\n",
    "from keras.layers import LSTM\n",
    "from sklearn import metrics\n",
    "\n",
    "import time\n",
    "import dataframe_image as dfi\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import all functions from another notebook for building prediction method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import Setting\n",
    "from myfunctions import lr_model,svr_model,ann_model,rf_model,lstm_model,hybrid_ceemdan_rf,hybrid_ceemdan_lstm,proposed_method"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import parameter settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hours=Setting.n_hours\n",
    "data_partition=Setting.data_partition\n",
    "max_features=Setting.max_features\n",
    "epoch=Setting.epoch\n",
    "batch_size=Setting.batch_size\n",
    "neuron=Setting.neuron\n",
    "lr=Setting.lr\n",
    "optimizer=Setting.optimizer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run the experiments\n",
    "### Run this following cell will train and test the proposed method and other benchmark methods on Office Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- 0.2771303653717041 seconds - Linear Regression- office ---\n",
      "--- 0.28983283042907715 seconds - Support Vector Regression- office ---\n",
      "--- 1.6380853652954102 seconds - ANN- office ---\n",
      "--- 1.1623618602752686 seconds - Random Forest- office ---\n",
      "--- 6.343520641326904 seconds - lstm- office ---\n",
      "--- 32.36379551887512 seconds - ceemdan_rf- office ---\n",
      "--- 83.0352873802185 seconds - ceemdan_lstm- office ---\n",
      "--- 81.80333948135376 seconds - proposed_method- office ---\n"
     ]
    }
   ],
   "source": [
    "#Linear Regression\n",
    "\n",
    "start_time = time.time()\n",
    "lr_office=lr_model(datas_office,hours,data_partition)\n",
    "lr_time_office=time.time() - start_time\n",
    "print(\"--- %s seconds - Linear Regression- office ---\" % (lr_time_office))\n",
    "\n",
    "#Support Vector Regression\n",
    "start_time = time.time()\n",
    "svr_office=svr_model(datas_office,hours,data_partition)\n",
    "svr_time_office=time.time() - start_time\n",
    "print(\"--- %s seconds - Support Vector Regression- office ---\" % (svr_time_office))\n",
    "\n",
    "\n",
    "#ANN\n",
    "start_time = time.time()\n",
    "ann_office=ann_model(datas_office,hours,data_partition)\n",
    "ann_time_office=time.time() - start_time\n",
    "print(\"--- %s seconds - ANN- office ---\" % (ann_time_office))\n",
    "\n",
    "#random forest\n",
    "start_time = time.time()\n",
    "rf_office=rf_model(datas_office,hours,data_partition,max_features)\n",
    "rf_time_office=time.time() - start_time\n",
    "print(\"--- %s seconds - Random Forest- office ---\" % (rf_time_office))\n",
    "\n",
    "#LSTM\n",
    "start_time = time.time()\n",
    "lstm_office=lstm_model(datas_office,hours,data_partition,max_features,epoch,batch_size,neuron,lr,optimizer)\n",
    "lstm_time_office=time.time() - start_time\n",
    "print(\"--- %s seconds - lstm- office ---\" % (lstm_time_office))\n",
    "\n",
    "\n",
    "#CEEMDAN RF\n",
    "start_time = time.time()\n",
    "ceemdan_rf_office=hybrid_ceemdan_rf(dfs_office,hours,data_partition,max_features)\n",
    "ceemdan_rf_time_office=time.time() - start_time\n",
    "print(\"--- %s seconds - ceemdan_rf- office ---\" % (ceemdan_rf_time_office))\n",
    "\n",
    "#CEEMDAN LSTM\n",
    "start_time = time.time()\n",
    "ceemdan_lstm_office=hybrid_ceemdan_lstm(dfs_office,hours,data_partition,max_features,epoch,batch_size,neuron,lr,optimizer)\n",
    "ceemdan_lstm_time_office=time.time() - start_time\n",
    "print(\"--- %s seconds - ceemdan_lstm- office ---\" % (ceemdan_lstm_time_office))\n",
    "\n",
    "\n",
    "#proposed method\n",
    "start_time = time.time()\n",
    "proposed_method_office=proposed_method(dfs_office,hours,data_partition,max_features,epoch,batch_size,neuron,lr,optimizer)\n",
    "proposed_method_time_office=time.time() - start_time\n",
    "print(\"--- %s seconds - proposed_method- office ---\" % (proposed_method_time_office))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summarize of experimental results with running time\n",
    "### Run this following cell will summarize the result and generate output used in Section 4.4 (Table 3) for Office dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LR</th>\n",
       "      <th>SVR</th>\n",
       "      <th>ANN</th>\n",
       "      <th>RF</th>\n",
       "      <th>LSTM</th>\n",
       "      <th>CEEMDAN RF</th>\n",
       "      <th>CEEMDAN LSTM</th>\n",
       "      <th>Proposed Method</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>office results</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>MAPE(%)</th>\n",
       "      <td>11.169459</td>\n",
       "      <td>9.306992</td>\n",
       "      <td>11.291101</td>\n",
       "      <td>10.251288</td>\n",
       "      <td>9.792064</td>\n",
       "      <td>6.164850</td>\n",
       "      <td>6.353779</td>\n",
       "      <td>5.330695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RMSE</th>\n",
       "      <td>1.217669</td>\n",
       "      <td>1.106114</td>\n",
       "      <td>1.148319</td>\n",
       "      <td>1.083738</td>\n",
       "      <td>1.123522</td>\n",
       "      <td>0.649620</td>\n",
       "      <td>0.668548</td>\n",
       "      <td>0.570283</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MAE</th>\n",
       "      <td>0.901621</td>\n",
       "      <td>0.769583</td>\n",
       "      <td>0.876292</td>\n",
       "      <td>0.805058</td>\n",
       "      <td>0.805700</td>\n",
       "      <td>0.491275</td>\n",
       "      <td>0.499195</td>\n",
       "      <td>0.430380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>running time (s)</th>\n",
       "      <td>0.277130</td>\n",
       "      <td>0.289833</td>\n",
       "      <td>1.638085</td>\n",
       "      <td>1.162362</td>\n",
       "      <td>6.343521</td>\n",
       "      <td>32.363796</td>\n",
       "      <td>83.035287</td>\n",
       "      <td>81.803339</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         LR       SVR        ANN         RF      LSTM  \\\n",
       "office results                                                          \n",
       "MAPE(%)           11.169459  9.306992  11.291101  10.251288  9.792064   \n",
       "RMSE               1.217669  1.106114   1.148319   1.083738  1.123522   \n",
       "MAE                0.901621  0.769583   0.876292   0.805058  0.805700   \n",
       "running time (s)   0.277130  0.289833   1.638085   1.162362  6.343521   \n",
       "\n",
       "                  CEEMDAN RF  CEEMDAN LSTM  Proposed Method  \n",
       "office results                                               \n",
       "MAPE(%)             6.164850      6.353779         5.330695  \n",
       "RMSE                0.649620      0.668548         0.570283  \n",
       "MAE                 0.491275      0.499195         0.430380  \n",
       "running time (s)   32.363796     83.035287        81.803339  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "running_time_office=pd.DataFrame([lr_time_office,svr_time_office,ann_time_office,\n",
    "                                   rf_time_office,lstm_time_office,ceemdan_rf_time_office,\n",
    "                                   ceemdan_lstm_time_office,proposed_method_time_office])\n",
    "running_time_office=running_time_office.T\n",
    "running_time_office.columns=['LR','SVR','ANN','RF','LSTM','CEEMDAN RF','CEEMDAN LSTM','Proposed Method']\n",
    "\n",
    "\n",
    "proposed_method_office_df=proposed_method_office[0:3]\n",
    "result_office=pd.DataFrame([lr_office,svr_office,ann_office,rf_office,lstm_office,ceemdan_rf_office,\n",
    "                    ceemdan_lstm_office,proposed_method_office_df])\n",
    "result_office=result_office.T\n",
    "result_office.columns=['LR','SVR','ANN','RF','LSTM','CEEMDAN RF','CEEMDAN LSTM','Proposed Method']\n",
    "office_summary=pd.concat([result_office,running_time_office],axis=0)\n",
    "\n",
    "office_summary.set_axis(['MAPE(%)', 'RMSE','MAE','running time (s)'], axis='index')\n",
    "\n",
    "office_summary.style.set_caption(\"Office Results\")\n",
    "index = office_summary.index\n",
    "index.name = \"office results\"\n",
    "office_summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export table to png\n",
    "#dfi.export(office_summary,\"office_summary_table.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate percentage improvement\n",
    "### Run this following cell will calculate percentage improvement and generate output used in Section 4.4 (Table 4) for Office dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Proposed Method vs.LR</th>\n",
       "      <th>Proposed Method vs.SVR</th>\n",
       "      <th>Proposed Method vs.ANN</th>\n",
       "      <th>Proposed Method vs.RF</th>\n",
       "      <th>Proposed Method vs.LSTM</th>\n",
       "      <th>Proposed Method vs.CEEMDAN RF</th>\n",
       "      <th>Proposed Method vs. CEEMDAN LSTM</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Percentage Improvement office</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>MAPE(%)</th>\n",
       "      <td>52.27</td>\n",
       "      <td>42.72</td>\n",
       "      <td>52.79</td>\n",
       "      <td>48.00</td>\n",
       "      <td>45.56</td>\n",
       "      <td>13.53</td>\n",
       "      <td>16.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RMSE</th>\n",
       "      <td>53.17</td>\n",
       "      <td>48.44</td>\n",
       "      <td>50.34</td>\n",
       "      <td>47.38</td>\n",
       "      <td>49.24</td>\n",
       "      <td>12.21</td>\n",
       "      <td>14.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MAE</th>\n",
       "      <td>52.27</td>\n",
       "      <td>44.08</td>\n",
       "      <td>50.89</td>\n",
       "      <td>46.54</td>\n",
       "      <td>46.58</td>\n",
       "      <td>12.40</td>\n",
       "      <td>13.79</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               Proposed Method vs.LR  Proposed Method vs.SVR  \\\n",
       "Percentage Improvement office                                                  \n",
       "MAPE(%)                                        52.27                   42.72   \n",
       "RMSE                                           53.17                   48.44   \n",
       "MAE                                            52.27                   44.08   \n",
       "\n",
       "                                Proposed Method vs.ANN  Proposed Method vs.RF  \\\n",
       "Percentage Improvement office                                                   \n",
       "MAPE(%)                                          52.79                  48.00   \n",
       "RMSE                                             50.34                  47.38   \n",
       "MAE                                              50.89                  46.54   \n",
       "\n",
       "                               Proposed Method vs.LSTM  \\\n",
       "Percentage Improvement office                            \n",
       "MAPE(%)                                          45.56   \n",
       "RMSE                                             49.24   \n",
       "MAE                                              46.58   \n",
       "\n",
       "                               Proposed Method vs.CEEMDAN RF  \\\n",
       "Percentage Improvement office                                  \n",
       "MAPE(%)                                                13.53   \n",
       "RMSE                                                   12.21   \n",
       "MAE                                                    12.40   \n",
       "\n",
       "                               Proposed Method vs. CEEMDAN LSTM  \n",
       "Percentage Improvement office                                    \n",
       "MAPE(%)                                                   16.10  \n",
       "RMSE                                                      14.70  \n",
       "MAE                                                       13.79  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pMAPE_LR_vs_Proposed_office=((lr_office[0]-proposed_method_office[0])/lr_office[0])*100\n",
    "pRMSE_LR_vs_Proposed_office=((lr_office[1]-proposed_method_office[1])/lr_office[1])*100\n",
    "pMAE_LR_vs_Proposed_office=((lr_office[2]-proposed_method_office[2])/lr_office[2])*100\n",
    "\n",
    "pMAPE_SVR_vs_Proposed_office=((svr_office[0]-proposed_method_office[0])/svr_office[0])*100\n",
    "pRMSE_SVR_vs_Proposed_office=((svr_office[1]-proposed_method_office[1])/svr_office[1])*100\n",
    "pMAE_SVR_vs_Proposed_office=((svr_office[2]-proposed_method_office[2])/svr_office[2])*100\n",
    "\n",
    "pMAPE_ANN_vs_Proposed_office=((ann_office[0]-proposed_method_office[0])/ann_office[0])*100\n",
    "pRMSE_ANN_vs_Proposed_office=((ann_office[1]-proposed_method_office[1])/ann_office[1])*100\n",
    "pMAE_ANN_vs_Proposed_office=((ann_office[2]-proposed_method_office[2])/ann_office[2])*100\n",
    "\n",
    "pMAPE_RF_vs_Proposed_office=((rf_office[0]-proposed_method_office[0])/rf_office[0])*100\n",
    "pRMSE_RF_vs_Proposed_office=((rf_office[1]-proposed_method_office[1])/rf_office[1])*100\n",
    "pMAE_RF_vs_Proposed_office=((rf_office[2]-proposed_method_office[2])/rf_office[2])*100\n",
    "\n",
    "pMAPE_LSTM_vs_Proposed_office=((lstm_office[0]-proposed_method_office[0])/lstm_office[0])*100\n",
    "pRMSE_LSTM_vs_Proposed_office=((lstm_office[1]-proposed_method_office[1])/lstm_office[1])*100\n",
    "pMAE_LSTM_vs_Proposed_office=((lstm_office[2]-proposed_method_office[2])/lstm_office[2])*100\n",
    "\n",
    "pMAPE_ceemdan_rf_vs_Proposed_office=((ceemdan_rf_office[0]-proposed_method_office[0])/ceemdan_rf_office[0])*100\n",
    "pRMSE_ceemdan_rf_vs_Proposed_office=((ceemdan_rf_office[1]-proposed_method_office[1])/ceemdan_rf_office[1])*100\n",
    "pMAE_ceemdan_rf_vs_Proposed_office=((ceemdan_rf_office[2]-proposed_method_office[2])/ceemdan_rf_office[2])*100\n",
    "\n",
    "\n",
    "pMAPE_ceemdan_lstm_vs_Proposed_office=((ceemdan_lstm_office[0]-proposed_method_office[0])/ceemdan_lstm_office[0])*100\n",
    "pRMSE_ceemdan_lstm_vs_Proposed_office=((ceemdan_lstm_office[1]-proposed_method_office[1])/ceemdan_lstm_office[1])*100\n",
    "pMAE_ceemdan_lstm_vs_Proposed_office=((ceemdan_lstm_office[2]-proposed_method_office[2])/ceemdan_lstm_office[2])*100\n",
    "\n",
    "\n",
    "df_PI_office=[[pMAPE_LR_vs_Proposed_office,pMAPE_SVR_vs_Proposed_office,pMAPE_ANN_vs_Proposed_office,\n",
    "                pMAPE_RF_vs_Proposed_office,pMAPE_LSTM_vs_Proposed_office,pMAPE_ceemdan_rf_vs_Proposed_office,\n",
    "                pMAPE_ceemdan_lstm_vs_Proposed_office],\n",
    "                [pRMSE_LR_vs_Proposed_office,pRMSE_SVR_vs_Proposed_office,pRMSE_ANN_vs_Proposed_office,\n",
    "                pRMSE_RF_vs_Proposed_office,pRMSE_LSTM_vs_Proposed_office,pRMSE_ceemdan_rf_vs_Proposed_office,\n",
    "                pRMSE_ceemdan_lstm_vs_Proposed_office],\n",
    "                [pMAE_LR_vs_Proposed_office,pMAE_SVR_vs_Proposed_office,pMAE_ANN_vs_Proposed_office,\n",
    "                pMAE_RF_vs_Proposed_office,pMAE_LSTM_vs_Proposed_office,pMAE_ceemdan_rf_vs_Proposed_office,\n",
    "                pMAE_ceemdan_lstm_vs_Proposed_office]]\n",
    "\n",
    "PI_office=pd.DataFrame(df_PI_office, columns=[\"Proposed Method vs.LR\", \"Proposed Method vs.SVR\",\" Proposed Method vs.ANN\",\n",
    "                                      \"Proposed Method vs.RF\",\"Proposed Method vs.LSTM\",\"Proposed Method vs.CEEMDAN RF\",\n",
    "                                      \"Proposed Method vs. CEEMDAN LSTM\"])\n",
    "PI_office= PI_office.round(decimals = 2)\n",
    "PI_office.set_axis(['MAPE(%)', 'RMSE','MAE'], axis='index')\n",
    "PI_office.style.set_caption(\"Percentage Improvement-Office Building\")\n",
    "index = PI_office.index\n",
    "index.name = \"Percentage Improvement office\"\n",
    "PI_office"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export table to png\n",
    "#dfi.export(PI_office,\"PI_office_table.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
